# local_file_storage.py
import json
import os
import csv

class LocalFileStorage:
    def __init__(self, output_dir):
        self.output_dir = output_dir
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)

    def save_json(self, data, filename):
        filepath = os.path.join(self.output_dir, filename)
        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=4)
        print(f"数据已保存到 {filepath} 文件中")

    def save_csv(self, job_list, filename):
        filepath = os.path.join(self.output_dir, filename)
        fieldnames = [
            'securityId', 'bossAvatar', 'bossCert', 'encryptBossId', 'bossName',
            'bossTitle', 'goldHunter', 'bossOnline', 'encryptJobId', 'expectId',
            'jobName', 'lid', 'salaryDesc', 'jobLabels', 'jobValidStatus', 'iconWord',
            'skills', 'jobExperience', 'daysPerWeekDesc', 'leastMonthDesc', 'jobDegree',
            'cityName', 'areaDistrict', 'businessDistrict', 'jobType', 'proxyJob',
            'proxyType', 'anonymous', 'outland', 'optimal', 'iconFlagList', 'itemId',
            'city', 'isShield', 'atsDirectPost', 'gps', 'encryptBrandId', 'brandName',
            'brandLogo', 'brandStageName', 'brandIndustry', 'brandScaleName', 'welfareList',
            'industry', 'contact', 'showTopPosition'
        ]

        with open(filepath, 'w', newline='', encoding='utf-8-sig') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()
            for job in job_list:
                job['welfareList'] = ', '.join(job['welfareList'])
                job['skills'] = ', '.join(job['skills'])
                writer.writerow({
                    'securityId': job.get('securityId', ''),
                    'bossAvatar': job.get('bossAvatar', ''),
                    'bossCert': job.get('bossCert', ''),
                    'encryptBossId': job.get('encryptBossId', ''),
                    'bossName': job.get('bossName', ''),
                    'bossTitle': job.get('bossTitle', ''),
                    'goldHunter': job.get('goldHunter', ''),
                    'bossOnline': job.get('bossOnline', ''),
                    'encryptJobId': job.get('encryptJobId', ''),
                    'expectId': job.get('expectId', ''),
                    'jobName': job.get('jobName', ''),
                    'lid': job.get('lid', ''),
                    'salaryDesc': job.get('salaryDesc', ''),
                    'jobLabels': job.get('jobLabels', ''),
                    'jobValidStatus': job.get('jobValidStatus', ''),
                    'iconWord': job.get('iconWord', ''),
                    'skills': job['skills'],
                    'jobExperience': job.get('jobExperience', ''),
                    'daysPerWeekDesc': job.get('daysPerWeekDesc', ''),
                    'leastMonthDesc': job.get('leastMonthDesc', ''),
                    'jobDegree': job.get('jobDegree', ''),
                    'cityName': job.get('cityName', ''),
                    'areaDistrict': job.get('areaDistrict', ''),
                    'businessDistrict': job.get('businessDistrict', ''),
                    'jobType': job.get('jobType', ''),
                    'proxyJob': job.get('proxyJob', ''),
                    'proxyType': job.get('proxyType', ''),
                    'anonymous': job.get('anonymous', ''),
                    'outland': job.get('outland', ''),
                    'optimal': job.get('optimal', ''),
                    'iconFlagList': job.get('iconFlagList', ''),
                    'itemId': job.get('itemId', ''),
                    'city': job.get('city', ''),
                    'isShield': job.get('isShield', ''),
                    'atsDirectPost': job.get('atsDirectPost', ''),
                    'gps': job.get('gps', ''),
                    'encryptBrandId': job.get('encryptBrandId', ''),
                    'brandName': job.get('brandName', ''),
                    'brandLogo': job.get('brandLogo', ''),
                    'brandStageName': job.get('brandStageName', ''),
                    'brandIndustry': job.get('brandIndustry', ''),
                    'brandScaleName': job.get('brandScaleName', ''),
                    'welfareList': job['welfareList'],
                    'industry': job.get('industry', ''),
                    'contact': job.get('contact', ''),
                    'showTopPosition': job.get('showTopPosition', '')
                })
        print(f"数据已保存到 {filepath} 文件中")
